package day14;

import java.io.*;

public class testserializable01 {
    public static void main(String[] args) {
        method();
        method2();
    }

    private static void method() {
        ObjectOutputStream out=null;
        try {
            Student s=new Student("wang",15);
            out=new ObjectOutputStream(new FileOutputStream("G:\\ready\\1.txt"));
            out.writeObject(s);
            System.out.println("序列化成功");
        } catch (IOException e) {
            System.out.println("序列化失败");
            e.printStackTrace();
        }finally {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void method2() {
        ObjectInputStream in=null;
        try {
            in=new ObjectInputStream(new FileInputStream("G:\\ready\\1.txt"));
            Object o = in.readObject();
            System.out.println(o);
            System.out.println("反序列化成功");
        } catch (Exception e) {
            System.out.println("反序列化失败");
            e.printStackTrace();
        }finally {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
